﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmNxDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmNx_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Nx", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmNx_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Nx", row["Ma_Nx"]), new SqlParameter("p_Ten_Nx", row["Ten_Nx"]), new SqlParameter("p_Ten_Nx_E", row["Ten_Nx_E"]), new SqlParameter("p_Post_GL", row["Post_GL"]), new SqlParameter("p_Ct_Nx", row["Ct_Nx"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Ct_Kt", row["Ct_Kt"]), new SqlParameter("p_Ma_Ct_Kt", row["Ma_Ct_Kt"]), new SqlParameter("p_XNK_Ck", row["XNK_Ck"]), new SqlParameter("p_Tk_No3_XNK", row["Tk_No3_XNK"]), new SqlParameter("p_Tk_Co3_XNK", row["Tk_Co3_XNK"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), 
                new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x10].Direction = ParameterDirection.Output;
            values[0x10].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmNx_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[0x10].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

